﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace FastDeal.Control
{
    public partial class wuc_KhuyenMaiTheoDiaDiem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "ohMua! Danh Sách Theo Khu Vực";
            try
            {
                int madd = int.Parse(Request.QueryString["MaDiaPhuong"].ToString());

                DanhSachKhuyenMaiTheoDiaDiem(madd);
            }
            catch
            {
                DanhSachKhuyenMaiTheoDiaDiem(0);
            }

            try
            {
                int ma = (int)Session["MaDiaPhuong"];
                ((DropDownList)this.FindControl("DropDownList1")).SelectedValue = ma.ToString();

            }
            catch
            {
            }
        }

        private void DanhSachKhuyenMaiTheoDiaDiem(int madd)
        {
            IQueryable madd1;
            DiaDiem_KhuyenMai_BUS db = new DiaDiem_KhuyenMai_BUS();
            madd1 = db.KhuyenMaiTheoDiaDiem(madd);
            DLKhuyenMai.DataSource = madd1;
            DLKhuyenMai.DataBind();

            if (DLKhuyenMai.Items.Count == 0)
            {
                Response.Redirect("~/HienThiTheoKhuVuc.aspx");
            }
            int i = 0;
            foreach (DataListItem dr in DLKhuyenMai.Items)
            {
                DateTime dtbatdau = DateTime.Parse((((Label)dr.FindControl("lbThoiGianBatDau")).Text));
                DateTime dtketthuc = DateTime.Parse((((Label)dr.FindControl("lbThoiGianKetThuc")).Text));
                int tongsonguoimua = int.Parse((((Label)dr.FindControl("lbSoLuongNguoiMua")).Text));
                int soluongtoida = int.Parse((((Label)dr.FindControl("lbSoLuongToiDa")).Text));

                if (dtketthuc > DateTime.Now && soluongtoida >= tongsonguoimua)
                {
                    ((Image)DLKhuyenMai.Items[i].FindControl("image_ChayHang")).Visible = true;
                }
                else
                {
                    ((Image)DLKhuyenMai.Items[i].FindControl("image_ChayHang")).Visible = false;
                }
                i = i + 1;
            }

            if (DLKhuyenMai.Items.Count < 5)
            {
                Panel1.Height = 800;
            }
        }

        protected void btnXem_Click(object sender, EventArgs e)
        {

        }
    }
}